package cn.nfwx.tv.adapter;

import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.LifecycleObserver;
import android.arch.lifecycle.OnLifecycleEvent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import cn.nfwx.tv.R;
import cn.nfwx.tv.controller.DownloadM3u8Controller;
import cn.nfwx.tv.db.AppDatabase;
import cn.nfwx.tv.db.DownProgramModel;
import cn.nfwx.tv.model.TvProgram;
import cn.nfwx.tv.util.LogUtil;
import cn.nfwx.tv.util.TimeUtil;

/**
 * 回看节目列表适配器
 * Created by zb.liu on 2017/11/2.
 */

public class LookBackProgramAdapter extends BaseQuickAdapter<TvProgram, BaseViewHolder> implements LifecycleObserver {
    String programUrl;

    public LookBackProgramAdapter() {
        super(R.layout.activity_lookback_program_item);
    }

    @Override
    protected void convert(BaseViewHolder helper, TvProgram model) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String hhmm = "";
        try {
            hhmm = TimeUtil.getFormatHHmmDate(dateFormat.parse(model.getTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        ((TextView) helper.getView(R.id.lookback_program_name)).setText(model.getProgram_name());
        ((TextView) helper.getView(R.id.lookback_program_time)).setText(hhmm);

        ((TextView) helper.getView(R.id.lookback_program_name)).setTextColor(Color.WHITE);
        ((TextView) helper.getView(R.id.lookback_program_time)).setTextColor(Color.WHITE);
        helper.getView(R.id.lookback_program_note).setVisibility(View.GONE);
        helper.getView(R.id.lookback_program_download_btn).setVisibility(View.VISIBLE);

        if (TextUtils.isEmpty(model.getUrl())) {
            helper.getView(R.id.lookback_program_download_btn).setVisibility(View.INVISIBLE);
        } else {
            helper.getView(R.id.lookback_program_download_btn).setVisibility(View.VISIBLE);
        }

        int type = 0;

        try {
            Date date = dateFormat.parse(model.getTime());
            if (date.getTime() <= System.currentTimeMillis()) {
                if ((helper.getLayoutPosition() + 1) < getData().size()) {
                    Object object1 = getData().get(helper.getLayoutPosition() + 1);
                    TvProgram tvProgram1 = (TvProgram) object1;
                    Date date1 = dateFormat.parse(tvProgram1.getTime());
                    if (date1.getTime() > System.currentTimeMillis()) {
                        type = 1;
                    } else {
                        type = 0;
                    }
                } else {
                    if (date.getTime() < System.currentTimeMillis()) {
                        String t1 = TimeUtil.getFormat2Time(date.getTime() / 1000L);
                        String t2 = TimeUtil.getFormat2Time(System.currentTimeMillis() / 1000L);
                        if (TextUtils.isEmpty(model.getUrl()) && t1.equals(t2)) {
                            type = 1;
                        } else {
                            type = 0;
                        }
                    } else if (date.getTime() == System.currentTimeMillis()) {
                        type = 1;
                    } else {
                        type = 2;
                    }
                }
            } else {
                type = 2;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        int text = R.string.download;

        model.setType(type);
        switch (type) {
            case 0:
                if (TextUtils.isEmpty(model.getUrl())) {
                    text = R.string.process;
                    ((TextView)helper.getView(R.id.lookback_program_note)).setText(text);
                    helper.getView(R.id.lookback_program_note).setVisibility(View.VISIBLE);
                    helper.getView(R.id.lookback_program_download_btn).setVisibility(View.GONE);
                } else {
                    int programStats = DownloadM3u8Controller.getInstance().getProgramStats(mContext, model.getUrl());
                    if (programStats == -1) {
                        helper.getView(R.id.lookback_program_note).setVisibility(View.GONE);
                        helper.getView(R.id.lookback_program_download_btn).setVisibility(View.VISIBLE);
                    } else if (programStats == DownProgramModel.STATS_SUCCESS) {
                        text = R.string.downloaded;
                        ((TextView)helper.getView(R.id.lookback_program_note)).setText(text);
                        helper.getView(R.id.lookback_program_note).setVisibility(View.VISIBLE);
                        helper.getView(R.id.lookback_program_download_btn).setVisibility(View.GONE);
                    } else {
                        text = R.string.downloading;
                        ((TextView)helper.getView(R.id.lookback_program_note)).setText(text);
                        helper.getView(R.id.lookback_program_note).setVisibility(View.VISIBLE);
                        helper.getView(R.id.lookback_program_download_btn).setVisibility(View.GONE);
                    }
                }
                ((TextView) helper.getView(R.id.lookback_program_name)).setTextColor(Color.WHITE);
                ((TextView) helper.getView(R.id.lookback_program_time)).setTextColor(Color.WHITE);
                ((TextView) helper.getView(R.id.lookback_program_note)).setTextColor(Color.WHITE);
                helper.getView(R.id.lookback_program_download_btn).setSelected(false);
                break;
            case 1:
                text = R.string.liveing;
                ((TextView) helper.getView(R.id.lookback_program_name)).setTextColor(Color.WHITE);
                ((TextView) helper.getView(R.id.lookback_program_time)).setTextColor(Color.WHITE);
                ((TextView) helper.getView(R.id.lookback_program_note)).setTextColor(Color.WHITE);
                helper.getView(R.id.lookback_program_note).setVisibility(View.VISIBLE);
                ((TextView) helper.getView(R.id.lookback_program_note)).setText(text);
                helper.getView(R.id.lookback_program_download_btn).setVisibility(View.GONE);
                break;
            case 2:
                boolean isEmpty = AppDatabase.getInstance(mContext).appointDao().queryAllById(model.getProgram_id()).isEmpty();
                if (model.isHasAppoint() || !isEmpty) {
                    text = R.string.appiontmented;
                    ((TextView) helper.getView(R.id.lookback_program_note)).setText(text);
                    helper.getView(R.id.lookback_program_note).setVisibility(View.VISIBLE);
                    ((TextView) helper.getView(R.id.lookback_program_name)).setTextColor(Color.parseColor("#999999"));
                    ((TextView) helper.getView(R.id.lookback_program_time)).setTextColor(Color.parseColor("#999999"));
                    ((TextView) helper.getView(R.id.lookback_program_note)).setTextColor(Color.parseColor("#999999"));
                    helper.getView(R.id.lookback_program_download_btn).setVisibility(View.GONE);
                } else {
                    text = R.string.appiontment;
                    helper.getView(R.id.lookback_program_download_btn).setVisibility(View.VISIBLE);
                    ((TextView) helper.getView(R.id.lookback_program_name)).setTextColor(Color.parseColor("#999999"));
                    ((TextView) helper.getView(R.id.lookback_program_time)).setTextColor(Color.parseColor("#999999"));
                    ((TextView) helper.getView(R.id.lookback_program_note)).setTextColor(Color.parseColor("#999999"));
                    helper.getView(R.id.lookback_program_download_btn).setSelected(false);
                }
                break;
            default:
                break;
        }

        ((TextView)helper.getView(R.id.lookback_program_download_btn)).setText(text);

        if (TextUtils.isEmpty(programUrl)) {
            switch (type) {
                case 1:
                    ((TextView) helper.getView(R.id.lookback_program_name)).setTextColor(Color.parseColor("#FF5454"));
                    ((TextView) helper.getView(R.id.lookback_program_time)).setTextColor(Color.parseColor("#FF5454"));
                    ((TextView) helper.getView(R.id.lookback_program_note)).setTextColor(Color.parseColor("#FF5454"));
                    helper.getView(R.id.lookback_program_download_btn).setSelected(true);
                    helper.getView(R.id.lookback_program_note).setVisibility(View.VISIBLE);
                    break;
            }
        } else {
            if (!TextUtils.isEmpty(model.getUrl()) && programUrl.equals(model.getUrl())) {
                ((TextView) helper.getView(R.id.lookback_program_name)).setTextColor(Color.parseColor("#FF5454"));
                ((TextView) helper.getView(R.id.lookback_program_time)).setTextColor(Color.parseColor("#FF5454"));
                ((TextView) helper.getView(R.id.lookback_program_note)).setTextColor(Color.parseColor("#FF5454"));
                helper.getView(R.id.lookback_program_download_btn).setSelected(true);
            }
        }

        helper.addOnClickListener(R.id.lookback_program_download_btn);
        helper.addOnClickListener(R.id.lookback_program_name);
        helper.addOnClickListener(R.id.lookback_program_time);
    }


    public void setProgram(String programUrl) {
        this.programUrl = programUrl;
    }


    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    public void stop() {
        setNewData(new ArrayList<TvProgram>());
    }
}
